//
//  MessageCell.m
//  AirQuality
//
//  Created by Po on 15/9/17.
//  Copyright © 2015年 Po. All rights reserved.
//

#import "MessageCell.h"
#import "UIImageView+WebCache.h"
@interface MessageCell ()

@property (retain, nonatomic) NSArray * imagesArray;

@end

@implementation MessageCell

- (void)awakeFromNib {

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setTextHeight:(CGFloat)textHeight imagesHeight:(CGFloat)imageHeight images:(NSArray *)images;
{
    CGRect rect = self.frame;
    rect.size.height = 80 + textHeight + imageHeight;
    [self setFrame:rect];
    _detailHeight.constant = textHeight;
    _imageContentHeight.constant = imageHeight;
    _imagesArray = images;
    [self updateImages:images];
}

/**
 *  更新图片
 */
- (void)updateImages:(NSArray *)images
{
    
    [_imageViewArray removeAllObjects];
    _imageViewArray = [NSMutableArray array];
    for (UIView * view in _imageContentView.subviews) {
        [view removeFromSuperview];
    }
    
    for (NSUInteger i = 0; i < images.count; i ++) {
        CGRect rect = CGRectMake(90 * (i % 3),
                                 90 * (i / 3),
                                 80,
                                 80);
        UIImageView * detailImage = [[UIImageView alloc] initWithFrame:rect];
        [detailImage sd_setImageWithURL:[NSURL URLWithString:images[i]] placeholderImage:nil];
        [detailImage setTag:600 + i];
        UIControl * control = [[UIControl alloc] initWithFrame:rect];
        [control setTag:1000 + i];
        [_imageContentView addSubview:control];
        [control addTarget:self action:@selector(selectImage:) forControlEvents:UIControlEventTouchUpInside];
        [_imageContentView addSubview:detailImage];
        [_imageViewArray addObject:detailImage];
    }
}

#pragma mark - event
- (void)selectImage:(UIControl *)control
{
    NSInteger count = control.tag - 1000;
    if (_delegate) {
        CGRect rect = control.frame;
        //计算图片位于tableView的位置
        rect.origin.x += _imageContentView.frame.origin.x;
        rect.origin.y += (_imageContentView.frame.origin.y + self.frame.origin.y);
        
        UIImageView * imageView = _imageViewArray[count];
        UIImage * image = imageView.image;
        CGSize size = image.size;
        [_delegate MessageCell:self didPressImageViewCount:count imageSize:size images:_imagesArray];
    }
}
@end